package com.xiaomili.halfsugar.fragment;


import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;
import com.xiaomili.halfsugar.R;
import com.xiaomili.halfsugar.adapter.CommonHolder;
import com.xiaomili.halfsugar.adapter.CommonRecyclerViewAdapter;
import com.xiaomili.halfsugar.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * A simple {@link Fragment} subclass.
 */
public class ComCategoryFragment extends Fragment {
    private static final String KEY = "key";
    @InjectView(R.id.recycler_view)
    RecyclerView mRecyclerView;
    private Context mContext;
    private List<String> mDatas;

    public static ComCategoryFragment newInstance(ArrayList<String> datas) {
        Bundle args = new Bundle();
        args.putStringArrayList(KEY, datas);
        ComCategoryFragment fragment = new ComCategoryFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public ComCategoryFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
        mDatas = new ArrayList<>();
        if (getArguments() != null) {
            mDatas.addAll(getArguments().getStringArrayList(KEY));
            LogUtils.i("mDatas.size" + mDatas.size());
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_com_category, container, false);
        ButterKnife.inject(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 2));
        mRecyclerView.setAdapter(new CommonRecyclerViewAdapter<String>(mContext, mDatas, R.layout.item_recycler_com_product_category) {
            @Override
            public void bindData(final CommonHolder holder, final int position, String bean) {
                LogUtils.i("线程?" + Thread.currentThread().getName());
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        ImageView imageView = holder.findViewById(R.id.iv_item);
                        Picasso.with(mContext).load(mDatas.get(position)).into(imageView);
                    }
                });

            }
        });
    }

    Handler mHandler = new Handler();

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }
}
